package com.codedancer.campus.manager.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.common.exception.BusinessException;
import com.codedancer.campus.common.utils.UUIDGenerator;
import com.codedancer.campus.manager.service.IFeedbackRecordTypeService;
import com.codedancer.campus.manager.service.bo.feedbackRecordType.FeedbackRecordTypeAddOrUpdateBo;
import com.codedancer.campus.manager.service.bo.feedbackRecordType.FeedbackRecordTypePageSearchBo;
import com.codedancer.campus.manager.service.vo.feedbackRecordType.FeedbackRecordTypePageVo;
import com.codedancer.campus.mapper.FeedbackRecordMapper;
import com.codedancer.campus.mapper.FeedbackRecordTypeMapper;
import com.codedancer.campus.po.FeedbackRecord;
import com.codedancer.campus.po.FeedbackRecordType;
import com.codedancer.campus.qo.feedbackRecordType.FeedbackRecordTypePageSearchQo;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class FeedbackRecordTypeServiceImpl implements IFeedbackRecordTypeService {

    @Autowired
    private FeedbackRecordTypeMapper feedbackRecordTypeMapper;
    @Autowired
    private FeedbackRecordMapper feedbackRecordMapper;

    /**
     * 添加反馈记录类型
     *
     * @param feedbackRecordTypeAddOrUpdateBo
     */
    @Override
    public void add(FeedbackRecordTypeAddOrUpdateBo feedbackRecordTypeAddOrUpdateBo) {
        FeedbackRecordType feedbackRecordType = BeanUtil.copyProperties(
            feedbackRecordTypeAddOrUpdateBo, FeedbackRecordType.class);
        feedbackRecordType.setUuid(UUIDGenerator.getUUID());
        feedbackRecordTypeMapper.insert(feedbackRecordType);
    }

    /**
     * 修改反馈记录类型
     *
     * @param feedbackRecordTypeAddOrUpdateBo
     */
    @Override
    public void update(FeedbackRecordTypeAddOrUpdateBo feedbackRecordTypeAddOrUpdateBo) {
        FeedbackRecordType feedbackRecordType = feedbackRecordTypeMapper.findById(
                feedbackRecordTypeAddOrUpdateBo.getUuid())
            .orElseThrow(() -> new BusinessException("反馈记录类型不存在"));
        feedbackRecordType.setName(feedbackRecordTypeAddOrUpdateBo.getName());
        feedbackRecordTypeMapper.updateById(feedbackRecordType);
    }

    /**
     * 删除反馈记录类型
     *
     * @param uuid
     */
    @Override
    public void delete(String uuid) {
        // 先判断是否存在该类型被使用
        if (feedbackRecordMapper.existsByColumn("feedback_record_type_uuid", uuid)) {
            throw new BusinessException("该反馈记录类型已被使用，不能删除");
        }
        feedbackRecordTypeMapper.deleteById(uuid);
    }

    /**
     * 分页查询反馈记录类型
     *
     * @param feedbackRecordTypePageSearchBo
     * @return
     */
    @Override
    public IPage<FeedbackRecordTypePageVo> page(
        FeedbackRecordTypePageSearchBo feedbackRecordTypePageSearchBo) {
        FeedbackRecordTypePageSearchQo feedbackRecordTypePageSearchQo = BeanUtil.copyProperties(
            feedbackRecordTypePageSearchBo, FeedbackRecordTypePageSearchQo.class);
        IPage<FeedbackRecordType> feedbackRecordTypeIPage = feedbackRecordTypeMapper.page(
            new Page<FeedbackRecordType>(feedbackRecordTypePageSearchBo.getPage(),
                feedbackRecordTypePageSearchBo.getSize()), feedbackRecordTypePageSearchQo);
        IPage<FeedbackRecordTypePageVo> feedbackRecordTypePageVoIPage = new Page<>();
        List<FeedbackRecordTypePageVo> feedbackRecordTypePageVos;
        feedbackRecordTypePageVos = feedbackRecordTypeIPage.getRecords().stream().map(
            feedbackRecordType -> BeanUtil.copyProperties(feedbackRecordType,
                FeedbackRecordTypePageVo.class)).collect(
            Collectors.toList());
        feedbackRecordTypePageVoIPage.setRecords(feedbackRecordTypePageVos);
        feedbackRecordTypePageVoIPage.setTotal(feedbackRecordTypeIPage.getTotal());
        return feedbackRecordTypePageVoIPage;
    }
}
